<script setup lang="ts">
import { ref } from "vue";
import DeviceCategoryApi from "@/api/DeviceCategoryApi";
import { useLocalStore } from "@/views/report/device/store";
import { exportAllDevices } from "@/api/pipe";
import fileDownload from "js-file-download";
const props = defineProps({
  statisticsCategory: Number,
  categoryName: String,
  deviceCategoryList: Object,
});
const store = useLocalStore();
const pipelineTypeList = ref([]);
const pipePointTypeList = ref([]);
const totalLength = ref(0);
const totalNum = ref(0);

const getPipelineTypeList = () => {
  const params = {
    pageSize: 100,
    pageIndex: 1,
    pClassId: "02",
    isPublish: true,
  };
  DeviceCategoryApi.api_query_line(params).then(({ data, succeeded }: any) => {
    if (succeeded) {
      pipelineTypeList.value = data.map((item) => {
        const color = props.deviceCategoryList.getLineColor(item.classId);
        return {
          color,
          ...item,
        };
      });
      const t = data.reduce((total, l) => total + l.length, 0);
      totalLength.value = (t / 1000).toFixed(2);
    }
  });
};

const getPipePointTypeList = () => {
  const params = {
    pageSize: 100,
    pageIndex: 1,
    pClassId: "01",
    isPublish: true
  };
  DeviceCategoryApi.api_query_point(params).then(({ data, succeeded }: any) => {
    if (succeeded) {
      pipePointTypeList.value = data.map((item) => {
        const iconUrl = props.deviceCategoryList.getIconUrl(item.classId);
        return {
          iconUrl,
          ...item,
        };
      });
      const t = data.reduce((total, l) => total + l.num, 0);
      totalNum.value = t;
    }
  });
};

const download = () => {
  exportAllDevices({}).then((res: any) => {
    fileDownload(res.data, "全部设备.xlsx");
  });
};

onMounted(() => {
  getPipelineTypeList();
  getPipePointTypeList();
});
</script>

<template>
  <div class="statisticsResult">
    <div class="header">全设备汇总统计</div>
    <div class="group">
      <div class="cell">
        <div>
          <el-icon class="pointIcon" style="top: 3px"><Paperclip /></el-icon>
          <span>{{ totalLength }}</span>
        </div>
        <span>管道总长(千米)</span>
      </div>
      <div class="cell">
        <div>
          <el-icon class="pointIcon" style="top: 3px"><Coin /></el-icon>
          <span>{{ totalNum }}</span>
        </div>
        <span>设备总数(个)</span>
      </div>
    </div>
    <div class="oneLevel">管点明显汇总（单位：个）</div>
    <div class="group">
      <div class="cell" :key="point.classId" v-for="point in pipePointTypeList">
        <div style="height: 20px">
          <img
            v-if="point.iconUrl"
            :src="point.iconUrl"
            class="pointIcon"
          /><span>{{ point.num }}</span>
        </div>
        <el-tooltip
          class="box-item"
          effect="dark"
          :content="point.className"
          placement="top"
        >
          <span class="name">{{ point.className }}</span>
        </el-tooltip>
      </div>
    </div>
    <div class="oneLevel">管段明细汇总（单位：千米）</div>
    <div class="group">
      <div class="cell" :key="line.classId" v-for="line in pipelineTypeList">
        <div>
          <span
            v-if="line.color"
            class="lineColor"
            :style="`background: ${line.color}`"
          ></span
          ><span>{{ (line.length / 1000).toFixed(2) }}</span>
        </div>
        <el-tooltip
          class="box-item"
          effect="dark"
          :content="line.className"
          placement="top"
        >
          <span class="name">{{ line.className }}</span>
        </el-tooltip>
      </div>
    </div>
    <div class="footer">
      <el-button type="primary" @click="download">导出表格</el-button>
    </div>
  </div>
</template>

<style scoped lang="scss">
.statisticsResult {
  position: absolute;
  top: 65px;
  right: 20px;
  z-index: 1000;
  width: 250px;
  height: 780px;
  font-family: PingFangSC-bold, sans-serif;
  color: #000;
  background-color: rgb(255 255 255 / 100%);

  .header {
    height: 40px;
    margin: 10px;
    font-size: 20px;
    font-weight: bold;
    line-height: 30px;
    color: #000;
    text-align: center;
    border-bottom: 1px solid rgb(187 187 187 / 30%);
  }

  .oneLevel {
    display: inline-block;
    margin: 15px 0 10px 10px;
    font-size: 16px !important;
    font-weight: bold;
  }

  .group {
    display: grid;
    grid-template-columns: auto auto;
    grid-column-gap: 10px;
    max-height: 230px;
    padding: 10px;
    overflow: hidden auto;
  }

  .cell {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-around;
    width: 110px;
    height: 60px;
    margin-bottom: 10px;
    font-size: 14px;
    background: #e8eaf1;

    .pointIcon {
      position: relative;
      top: 5px;
      left: -5px;
      width: 24px;
      height: 24px;
    }

    .lineColor {
      position: relative;
      top: -2px;
      left: -5px;
      display: inline-block;
      width: 24px;
      height: 4px;
    }
    .name {
      display: inline-block;
      width: 100px;
      text-align: center;
      overflow: hidden; //超出的文本隐藏
      text-overflow: ellipsis; //溢出用省略号显示
      white-space: nowrap; //溢出不换行
    }
  }

  .footer {
    position: absolute;
    bottom: 30px;
    display: flex;
    justify-content: center;
    width: 100%;
    margin-top: 30px;
  }
}
</style>
